package com.fg.exemplocrudengenhariadesoftware.daobd;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.apache.log4j.Logger;

import com.fg.exemplocrudengenhariadesoftware.model.Professor;

public class ProfessorDao {
	private JDBCUitl JDBCUitl;
	private Connection conexaoBD;
	Logger logger;
	
	public ProfessorDao() {
		JDBCUitl = new JDBCUitl();
		logger = Logger.getLogger(this.getClass().getName());
	}
	
	public void insere (Professor professor) throws ErroAoConectarBDException {
		PreparedStatement preparedStatement = null;
		String stringSQL = "";
		
		try {
			conexaoBD = JDBCUitl.getConexaoBd();
			if (conexaoBD != null) {
				stringSQL = "INSERT INTO tbPROFESSOR (MATRICULA, NOME, SENHA) VALUES (?, ?, ?)";
				logger.debug("insert:" + stringSQL);
				try {
					preparedStatement = conexaoBD.prepareStatement(stringSQL);
					preparedStatement.setInt(1, professor.getMatricula());
					preparedStatement.setString(2, professor.getNome());
					preparedStatement.setString(3, professor.getSenha());
					preparedStatement.executeUpdate();
				} catch (Exception e1) {
					logger.error("erro ao inserir Professor:" + professor.toString());
				}
			}
		} catch (Exception e) {
			throw new ErroAoConectarBDException();
		}
	}

	
	public void update (Professor professor) throws ErroAoConectarBDException {
		PreparedStatement preparedStatement = null;
		String stringSQL = "";
		
		try {
			conexaoBD = JDBCUitl.getConexaoBd();
			if (conexaoBD != null) {
				stringSQL = "update tbPROFESSOR set NOME = ?, SENHA = ? where matricula = ?";
				logger.debug("updatee:" + stringSQL);
				try {
					preparedStatement = conexaoBD.prepareStatement(stringSQL);
					preparedStatement.setString(1, professor.getNome());
					preparedStatement.setString(2, professor.getSenha());
					preparedStatement.setInt(3, professor.getMatricula());
					preparedStatement.executeUpdate();
				} catch (Exception e1) {
					logger.error("erro ao atualizar Professor:" + professor.toString());
				}
			}
		} catch (Exception e) {
			throw new ErroAoConectarBDException();
		}
	}
	
	public void delete (Professor professor) throws ErroAoConectarBDException {
		PreparedStatement preparedStatement = null;
		String stringSQL = "";
		
		try {
			conexaoBD = JDBCUitl.getConexaoBd();
			if (conexaoBD != null) {
				stringSQL = "delete tbPROFESSOR matricula = ?";
				logger.debug("delete:" + stringSQL);
				try {
					preparedStatement = conexaoBD.prepareStatement(stringSQL);
					preparedStatement.setInt(1, professor.getMatricula());
					preparedStatement.executeUpdate();
				} catch (Exception e1) {
					logger.error("erro ao excluir Professor:" + professor.toString());
				}
			}
		} catch (Exception e) {
			throw new ErroAoConectarBDException();
		}
	}

}
